// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/05/28 14:04:52
// ==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yunchang;
using UnityEngine;
using UnityEngine.UI;
using Table.Blo;
using Table.DataModel;

namespace AutoChess
{
    public class ChessPokedexPage : Page
    {
        public ChessPokedexChessDetail DetailPanel;
        public Transform PodexPanel;

        public ChessPokedexItem Prefab;
        public Transform Content;

        public Dropdown RareDropDown;
        public Dropdown GroupDropdown;
        public Dropdown TypeDropdown;

        private List<ChessPokedexItem> _allItemList = new List<ChessPokedexItem>();

        private List<int> _allRareList = new List<int>() { 1, 2, 3, 4, 5 };
        private List<TableAutoChessGroup> _allGroupList = new List<TableAutoChessGroup>();
        private List<TableAutoChessGroup> _allTypeList = new List<TableAutoChessGroup>();

        private int _curRareValue = 0;
        private int _curGroupValue = -1;
        private int _curTypeValue = -1;

        private int _curSelectHeroId;

        private bool inited = false;
        private void Init()
        {
            if (inited)
                return;
            inited = true;

            List<string> rareDescList = new List<string>();
            List<string> groupDescList = new List<string>();
            List<string> typeDescList = new List<string>();
            rareDescList.Add(TableTextBlo.GetText("AllRare"));
            groupDescList.Add(TableTextBlo.GetText("AllGroup"));
            typeDescList.Add(TableTextBlo.GetText("AllType"));

            var allTables = Table.Blo.AutoChessBlo.GetAllGroups();
            for (int i = 0; i < allTables.Count; i++)
            {
                var table = allTables[i];
                if(table.groupType == 0)
                {
                    _allGroupList.Add(table);
                    groupDescList.Add(table.groupDesc);
                }
                else if(table.groupType == 1)
                {
                    _allTypeList.Add(table);
                    typeDescList.Add(table.groupSkillName);
                }
            }
            for (int i = 0; i < 5; i++)
            {
                var tableRare = AutoChessBlo.GetTableRareByRare(i + 1);
                rareDescList.Add(tableRare.rareDesc);
            }

            RareDropDown.ClearOptions();
            RareDropDown.AddOptions(rareDescList);
            RareDropDown.onValueChanged.AddListener(OnRareDropDownValueChanged);
            GroupDropdown.ClearOptions();
            GroupDropdown.AddOptions(groupDescList);
            GroupDropdown.onValueChanged.AddListener(OnGroupDropDownValueChanged);
            TypeDropdown.ClearOptions();
            TypeDropdown.AddOptions(typeDescList);
            TypeDropdown.onValueChanged.AddListener(OnTypeDropDownValueChanged);

            var allTableHeros = new List<TableAutoChessHero>(AutoChessBlo.GetAllChessHeros());
            allTableHeros.Sort((l, r) =>
            {
                return l.rare.CompareTo(r.rare);
            });
            for (int i = 0; i < allTableHeros.Count; i++)
            {
                var tableHero = allTableHeros[i];
                var newItem = Instantiate<ChessPokedexItem>(Prefab, Content);
                newItem.SetData(tableHero);
                _allItemList.Add(newItem);
            }

            RefreshAllItems();
        }

        private void OpenWithHero()
        {
            if (Params == null)
                return;

            int heroId = (int)Params;
            foreach (var item in _allItemList)
            {
                if (item.HeroId == heroId)
                {
                    var tableHero = AutoChessBlo.GetChessHero(heroId);
                    OnSelectHero(tableHero);
                    break;
                }
            }
        }

        protected override void DoOpen()
        {
            base.DoOpen();
            Init();
            OpenWithHero();
        }

        public void OnSelectHero(TableAutoChessHero tableData)
        {
            DetailPanel.ShowPanel(tableData);
            PodexPanel.gameObject.SetActive(false);
            _curSelectHeroId = tableData.heroId;
        }

        public void SelectNext(bool next)
        {
            int nextHeroId = _curSelectHeroId;
            int curIndex = 0;
            for (int i = 0; i < _allItemList.Count; i++)
            {
                var item = _allItemList[i];
                if (item.HeroId == _curSelectHeroId)
                {
                    curIndex = i;
                    break;
                }
            }

            int index = curIndex;
            do
            {
                index = next ? ++index : --index;
                if (index >= _allItemList.Count)
                    index = 0;
                else if (index < 0)
                    index = _allItemList.Count - 1;

                if (_allItemList[index].gameObject.activeSelf)
                {
                    nextHeroId = _allItemList[index].HeroId;
                    break;
                }
            } while (curIndex != index);

            if(nextHeroId != _curSelectHeroId)
            {
                var tableHero = AutoChessBlo.GetChessHero(nextHeroId);
                OnSelectHero(tableHero);
            }
        }

        public void ManualClose()
        {
            if(DetailPanel.gameObject.activeSelf)
            {
                DetailPanel.HidePanel();
                PodexPanel.gameObject.SetActive(true);
            }
            else
            {
                Close();
            }
        }

        //protected override void DoClose(bool immediately)
        //{
        //    base.DoClose(immediately);
        //}

        private void RefreshAllItems()
        {
            int activeNum = 0;
            for (int i = 0; i < _allItemList.Count; i++)
            {
                var item = _allItemList[i];
                bool choosed = item.IsChoosedItem(_curRareValue, _curGroupValue, _curTypeValue);
                item.gameObject.SetActive(choosed);
                if (choosed)
                    ++activeNum;
                item.LineObj.SetActive(activeNum % 5 == 1);
            }
        }

        private void OnRareDropDownValueChanged(int value)
        {
            _curRareValue = value;
            RefreshAllItems();
        }

        private void OnGroupDropDownValueChanged(int value)
        {
            if (value == 0)
                _curGroupValue = -1;
            else
            {
                var table = _allGroupList[value - 1];
                _curGroupValue = table.acHeroGroup;
            }
            RefreshAllItems();
        }

        private void OnTypeDropDownValueChanged(int value)
        {
            if (value == 0)
                _curTypeValue = -1;
            else
            {
                var table = _allTypeList[value - 1];
                _curTypeValue = table.acHeroGroup;
            }
            RefreshAllItems();
        }
    }
}
